package android.system.utils;

import java.util.ArrayList;

import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.system.SysAplication;
import android.system.data.DataInfo;
import android.system.data.ReceiptInfo;

public class DBOperate {
	private Database mDatabase = null;
	public static final int TYPE_SEND = 1;
	public static final int TYPE_RECEIVE = 2;

	public DBOperate() {
		mDatabase = SysAplication.getMe().getDB();
	}

	/***
	 * type 1收到手机发来的短信 2为收到网络发来的短信
	 * */
	public void InsertDataInfo(DataInfo info, int type) {
		StringBuilder sb = new StringBuilder();
		sb.append("insert into DataInfo(msg_id,to_telephone,for_telephone,content,time,status,type,flag) values (");
		sb.append(info.getMsgId());
		sb.append(",'");
		sb.append(info.getToTelephone());
		sb.append("','");
		sb.append(info.getForTelephone());
		sb.append("','");
		sb.append(info.getContent());
		sb.append("',");
		sb.append(info.getReviceTime());
		sb.append(",");
		sb.append(info.getStauts());
		sb.append(",");
		sb.append(type);
		sb.append(",");
		sb.append(info.getFlag());
		sb.append(")");
		mDatabase.execSql(sb.toString());
	}

	public void deleteDataInfo(int id) {
		StringBuilder sb = new StringBuilder();
		sb.append("delete from DataInfo where id=");
		sb.append(id);
		mDatabase.execSql(sb.toString());
	}
	public void deleteDataInfo(int id,int msgId) {
		StringBuilder sb = new StringBuilder();
		sb.append("delete from DataInfo where msg_id=");
		sb.append(msgId);
		mDatabase.execSql(sb.toString());
	}
	/*
	 * "[id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,"+
	 * "[msg_id] INTEGER  NOT NULL,"+ "[to_telephone] VARCHAR(30)  NOT NULL,"+
	 * "[for_telephone] VARCHAR(30)  NOT NULL,"+
	 * "[content]  VARCHAR(100) NULL,"+ "[time] INTEGER  NULL,"+
	 * "[status] INTEGER DEFAULT '0' NOT NULL,"+
	 * "[reserve_int3] INTEGER  NULL,"+ "[reserve_str1] VARCHAR(50)  NULL)";
	 */
	public DataInfo getSendDataInfo(long time) {
		String sql = "select * from DataInfo where time=" + time+" order by id desc";
		Cursor cursor = mDatabase.query(sql);
		DataInfo info = null;
		if (cursor != null) {
			if (cursor.getCount() != 0) {
				cursor.moveToFirst();
				info = new DataInfo();
				info.setId(cursor.getInt(cursor.getColumnIndex("id")));
				info.setMsgId(cursor.getInt(cursor.getColumnIndex("msg_id")));
				info.setContent(cursor.getString(cursor
						.getColumnIndex("content")));
				info.setToTelephone(cursor.getString(cursor
						.getColumnIndex("to_telephone")));
				info.setForTelephone(cursor.getString(cursor
						.getColumnIndex("for_telephone")));
				info.setReviceTime(cursor.getLong(cursor.getColumnIndex("time")));
				info.setStauts(cursor.getInt(cursor.getColumnIndex("status")));
				info.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
			}
			cursor.close();
			cursor = null;
		}
		return info;
	}
	public DataInfo getSendDataInfo(int  msgId) {
		String sql = "select * from DataInfo where msg_id=" + msgId;
		Cursor cursor = mDatabase.query(sql);
		DataInfo info = null;
		if (cursor != null) {
			if (cursor.getCount() != 0) {
				cursor.moveToFirst();
				info = new DataInfo();
				info.setId(cursor.getInt(cursor.getColumnIndex("id")));
				info.setMsgId(cursor.getInt(cursor.getColumnIndex("msg_id")));
				info.setContent(cursor.getString(cursor
						.getColumnIndex("content")));
				info.setToTelephone(cursor.getString(cursor
						.getColumnIndex("to_telephone")));
				info.setForTelephone(cursor.getString(cursor
						.getColumnIndex("for_telephone")));
				info.setReviceTime(cursor.getLong(cursor.getColumnIndex("time")));
				info.setStauts(cursor.getInt(cursor.getColumnIndex("status")));
				info.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
			}
			cursor.close();
			cursor = null;
		}
		return info;
	}
	public ArrayList<DataInfo> getSendDataInfos() {
		String sql = "select * from DataInfo where type=" + TYPE_SEND+" order by id desc";
		Cursor cursor = mDatabase.query(sql);
		ArrayList<DataInfo> list = new ArrayList<DataInfo>();
		if (cursor != null) {
			if (cursor.getCount() != 0) {
				while (cursor.moveToNext()) {
					DataInfo info = new DataInfo();
					info.setId(cursor.getInt(cursor.getColumnIndex("id")));
					info.setMsgId(cursor.getInt(cursor.getColumnIndex("msg_id")));
					info.setContent(cursor.getString(cursor
							.getColumnIndex("content")));
					info.setToTelephone(cursor.getString(cursor
							.getColumnIndex("to_telephone")));
					info.setForTelephone(cursor.getString(cursor
							.getColumnIndex("for_telephone")));
					info.setReviceTime(cursor.getLong(cursor
							.getColumnIndex("time")));
					info.setStauts(cursor.getInt(cursor
							.getColumnIndex("status")));
					info.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
					list.add(info);
				}
			}

			cursor.close();
			cursor = null;
		}
		return list;
	}

	public ArrayList<DataInfo> getReviceDataInfos() {
		String sql = "select * from DataInfo where type=" + TYPE_RECEIVE+" order by id desc";
		Cursor cursor = mDatabase.query(sql);
		ArrayList<DataInfo> list = new ArrayList<DataInfo>();
		if (cursor != null) {
			if (cursor.getCount() != 0) {
				while (cursor.moveToNext()) {
					DataInfo info = new DataInfo();
					info.setId(cursor.getInt(cursor.getColumnIndex("id")));
					info.setMsgId(cursor.getInt(cursor.getColumnIndex("msg_id")));
					info.setContent(cursor.getString(cursor
							.getColumnIndex("content")));
					info.setToTelephone(cursor.getString(cursor
							.getColumnIndex("to_telephone")));
					info.setForTelephone(cursor.getString(cursor
							.getColumnIndex("for_telephone")));
					info.setReviceTime(cursor.getLong(cursor
							.getColumnIndex("time")));
					info.setStauts(cursor.getInt(cursor
							.getColumnIndex("status")));
					info.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
					list.add(info);

				}
			}

			cursor.close();
			cursor = null;
		}
		return list;
	}

	/*
	 * private final String TABLE_RECEIPT_INFO= "CREATE TABLE [ReceiptInfo] ("+
	 * "[id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,"+
	 * "[msg_id] INTEGER  NOT NULL,"+ "[status] INTEGER DEFAULT '0' NOT NULL,"+
	 * "[type] INTEGER DEFAULT '-1' NOT NULL,"+ "[reserve_int2] INTEGER  NULL,"+
	 * "[reserve_int3] INTEGER  NULL,"+ "[reserve_str1] VARCHAR(50)  NULL)";
	 */
	public void InsertReceiptInfo(ReceiptInfo info) {
		StringBuilder sb = new StringBuilder();
		sb.append("insert into ReceiptInfo(msg_id,status) values (");
		sb.append(info.getMsgId());
		sb.append(",");
		sb.append(info.getStatus());
		sb.append(")");
		mDatabase.execSql(sb.toString());
	}

	public void deleteReceiptInfo(int id) {
		StringBuilder sb = new StringBuilder();
		sb.append("delete from ReceiptInfo where id=");
		sb.append(id);
		mDatabase.execSql(sb.toString());
	}

	public ArrayList<ReceiptInfo> getReceiptInfos() {
		String sql = "select * from ReceiptInfo";
		Cursor cursor = mDatabase.query(sql);
		ArrayList<ReceiptInfo> list = new ArrayList<ReceiptInfo>();
		if (cursor != null) {
			if (cursor.getCount() != 0) {
				while (cursor.moveToNext()) {
					ReceiptInfo info = new ReceiptInfo();
					info.setId(cursor.getInt(cursor.getColumnIndex("id")));
					info.setMsgId(cursor.getInt(cursor.getColumnIndex("msg_id")));
					info.setStatus(cursor.getInt(cursor
							.getColumnIndex("status")));
					list.add(info);

				}
			}

			cursor.close();
			cursor = null;
		}
		return list;
	}
	
	
	

}
